package icu.qx.qc.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.qc.domain.QcDrawbackType;
import icu.qx.qc.service.IQcDrawbackTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import icu.qx.common.utils.CodeUtils;


/**
 * 缺陷类型Controller
 *
 * @author ruoyi
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/qc/drawbackType")
public class QcDrawbackTypeController extends BaseController {
    @Autowired
    private IQcDrawbackTypeService qcDrawbackTypeService;
    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询缺陷类型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QcDrawbackType qcDrawbackType) {
        startPage();
        List<QcDrawbackType> list = qcDrawbackTypeService.selectQcDrawbackTypeList(qcDrawbackType);
        return getDataTable(list);
    }

    /**
     * 查询缺陷类型列表
     */
    @GetMapping("/all")
    public AjaxResult all(QcDrawbackType qcDrawbackType) {
        List<QcDrawbackType> list = qcDrawbackTypeService.selectQcDrawbackTypeList(qcDrawbackType);
        return success(list);
    }

    /**
     * 导出缺陷类型列表
     */
    @RequiresPermissions("qc:drawbackType:export")
    @Log(title = "缺陷类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcDrawbackType qcDrawbackType) {
        List<QcDrawbackType> list = qcDrawbackTypeService.selectQcDrawbackTypeList(qcDrawbackType);
        ExcelUtil<QcDrawbackType> util = new ExcelUtil<QcDrawbackType>(QcDrawbackType.class);
        util.exportExcel(response, list, "缺陷类型数据");
    }

    /**
     * 获取缺陷类型详细信息
     */
    @RequiresPermissions("qc:drawbackType:query")
    @GetMapping(value = "/{drawbackTypeCode}")
    public AjaxResult getInfo(@PathVariable("drawbackTypeCode") String drawbackTypeCode) {
        return success(qcDrawbackTypeService.selectQcDrawbackTypeByDrawbackTypeCode(drawbackTypeCode));
    }

    /**
     * 新增缺陷类型
     */
    @RequiresPermissions("qc:drawbackType:add")
    @Log(title = "缺陷类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QcDrawbackType qcDrawbackType) {
        qcDrawbackType.setDrawbackTypeCode(codeUtils.generateCode("qc_drawback_type"));
        qcDrawbackType.setCreateBy(SecurityUtils.getUsername());
        // 获取当前时间
        Date currentTime = new Date();
        qcDrawbackType.setCreateTime(currentTime);
        return toAjax(qcDrawbackTypeService.insertQcDrawbackType(qcDrawbackType));
    }


    /**
     * 修改缺陷类型
     */
    @RequiresPermissions("qc:drawbackType:edit")
    @Log(title = "缺陷类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QcDrawbackType qcDrawbackType) {
        qcDrawbackType.setUpdateBy(SecurityUtils.getUsername());
        // 获取当前时间
        Date currentTime = new Date();
        qcDrawbackType.setUpdateTime(currentTime);
        return toAjax(qcDrawbackTypeService.updateQcDrawbackType(qcDrawbackType));
    }

    /**
     * 删除缺陷类型
     */
    @RequiresPermissions("qc:drawbackType:remove")
    @Log(title = "缺陷类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drawbackTypeCodes}")
    public AjaxResult remove(@PathVariable String[] drawbackTypeCodes) {
        return toAjax(qcDrawbackTypeService.deleteQcDrawbackTypeByDrawbackTypeCodes(drawbackTypeCodes));
    }
}
